package com.lxmes.sales.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.sales.domain.NcClientFiles;
import com.lxmes.sales.domain.NcContract;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.sales.domain.NcSalesOrder;
import com.lxmes.sales.service.INcSalesOrderService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售正式订单Controller
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@RestController
@RequestMapping("/sales/salesorder")
public class NcSalesOrderController extends BaseController
{
    @Autowired
    private INcSalesOrderService ncSalesOrderService;

    /**
     * 查询销售正式订单列表
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcSalesOrder ncSalesOrder)
    {
        startPage();
        List<NcSalesOrder> list = ncSalesOrderService.selectNcSalesOrderList(ncSalesOrder);
        return getDataTable(list);
    }

    /**
     * 导出销售正式订单列表
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:export')")
    @Log(title = "销售正式订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcSalesOrder ncSalesOrder)
    {
        List<NcSalesOrder> list = ncSalesOrderService.selectNcSalesOrderList(ncSalesOrder);
        ExcelUtil<NcSalesOrder> util = new ExcelUtil<NcSalesOrder>(NcSalesOrder.class);
        util.exportExcel(response, list, "销售正式订单数据");
    }

    /**
     * 获取销售正式订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ncSalesOrderService.selectNcSalesOrderById(id));
    }

    /**
     * 新增销售正式订单
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:add')")
    @Log(title = "销售正式订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcSalesOrder ncSalesOrder)
    {
        return toAjax(ncSalesOrderService.insertNcSalesOrder(ncSalesOrder));
    }

    /**
     * 修改销售正式订单
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:edit')")
    @Log(title = "销售正式订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcSalesOrder ncSalesOrder)
    {
        return toAjax(ncSalesOrderService.updateNcSalesOrder(ncSalesOrder));
    }
    /**
     * 修改销售正式订单
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:edit')")
    @Log(title = "销售正式订单", businessType = BusinessType.UPDATE)
    @PutMapping("/splitSalesorder")
    public AjaxResult splitSalesorder(@RequestBody NcSalesOrder ncSalesOrder)
    {
        return toAjax(ncSalesOrderService.splitSalesorder(ncSalesOrder));
    }

    /**
     * 删除销售正式订单
     */
    @PreAuthorize("@ss.hasPermi('sales:salesorder:remove')")
    @Log(title = "销售正式订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncSalesOrderService.deleteNcSalesOrderByIds(ids));
    }

    @Log(title = "审核", businessType = BusinessType.DELETE)
    @PostMapping("/push/{ids}")
    public AjaxResult push(@PathVariable Long[] ids)
    {
//        for(long id : ids){
//            NcSalesOrder ncSalesOrder = ncSalesOrderService.selectNcSalesOrderById(id);
//            if("1".equals(ncSalesOrder.getPcState())){
//                return AjaxResult.error("您选择的订单中有已经排产的，请重新选择！");
//            }
//        }
        return toAjax(ncSalesOrderService.pushToPczb(ids));
    }


    @Log(title = "合同管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcSalesOrder> util = new ExcelUtil<>(NcSalesOrder.class);
        List<NcSalesOrder> orderList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncSalesOrderService.imporIteminfo(orderList, updateSupport, operName);

        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcSalesOrder> util = new ExcelUtil<>(NcSalesOrder.class);
        util.importTemplateExcel(response,"订单模板数据");
    }

}
